<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>流程处理</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
    <div class="Wrapper">
	    <div class="searchArea">
	    	<input name="isMore" id="isMore" type="text" style="display: none;" class="ui-textbox" value="${isMore}">
	        <form id="searchBar">
	            <div class="fieldset1 clearFix FBL5">
	                <div class="fieldBox">
	                    <div class="label">姓名</div>
	                    <div class="field">
	                        <input name="rlNm" id="rlNm" type="text" class="ui-textbox" value="" ligerui="width: 140">
	                    </div>
	                </div>
	                <div class="fieldBox">
	                    <div class="label">工号</div>
	                    <div class="field">
	                        <input name="usrNm" id="usrNm" type="text" class="ui-textbox" value="" ligerui="width: 140">
	                    </div>
	                </div>
	                    <span id="searchBtn" class="sq-button sq-button-round">查询</span>
	                    <span id="cleanFormBtn" class="sq-button sq-button-round">清除</span>
	            </div>
	        </form>
	    </div>
	    <table class="layout2col1">
	        <tbody>
	            <tr>
	                <td width="300" valign="top">
	                    <div class="sqPanel">
	                        <div class="sqPanel_head">
	                            <form id="searchBar2" class="titSearchBar">
	                                <div class="fieldset1 clearFix">
	                                    <div class="fieldBox">
	                                        <div class="label"><i class="sqIco sqIco_3"></i>组织</div>
	                                        <div class="field">
	                                             <input name="orgNm" id="orgNm" type="text" class="ui-textbox" value="" ligerui="width: 140">
	                                        </div>
	                                    </div>
	                                    <span id="searchBtn2" class="sq-search-button"></i>查询</span>
	                                </div>
	                            </form>
	                        </div>
	                        <div class="sqPanel_body">
	                            <div class="btnsBox">&nbsp;</div>
	                            <div class="nanoWrap" style="height: 330px;">
	                                <div class="nano"><div class="nano-content">
	                                    <ul id="tree1">
	                                    </ul>
	                                </div></div>
	                            </div>
	                        </div>
	                    </div>
	                </td>
	                <td valign="top">
	                    <div class="sqPanel">
	                        <div class="sqPanel_head">
	                            <span>检索结果</span>
	                        </div>
	                        <div class="sqPanel_body">
	                            <div id="ligergrid" class="sq-grid"></div>
	                             <div id="selecetedItems" class="textareaBox Hide" style="margin-top:10px; height:60px;"></div>
	                        </div>
	                    </div>
	                </td>
	            </tr>
	        </tbody>
	    </table>
	    <div class="btnsBox TC">
	        <span id="submitBtn" class="sq-button sq-button-round">确定</span>
	        <span id="clearAllBtn" class="sq-button sq-button-round">清除</span>
	        <span id="cancelBtn" class="sq-button sq-button-round"></i>关闭</span>
	    </div>
	</div>

<script>
    var R_url='../../_app/';
    var tree,
        grid,
        grid_addPE;
    	combo_DATA=[];
    $(function () {
    	render_ligergrid("");
    	initOrgTree();
    	initForm();
    	initSearchEvent();
    	initClearLigerFormEvent();
    	initSearchBarOrgForm();
    	initSearchBarOrgFormEvent();
    	initSubmitBtnEvent();
    	initClearAllBtnEvent();
    	initCancelBtnEvent();
    });
    
    //取消按钮
    function initCancelBtnEvent(){
    	$("#cancelBtn").click(function(event) {
         	frameElement.dialog.close();
        });
    }
    
    //初始化清除按钮Btn
    function initClearAllBtnEvent(){
    	$("#clearAllBtn").click(function(event) {
            $("#cleanFormBtn").click();
            tree.reload();
            if (grid) {
            	//grid.grid.hide();
            	reLoadGrid();
            } 
            change_combo_DATA("clear");
        });
    }
    
    //提交按钮事件
    function initSubmitBtnEvent(){
    	$("#submitBtn").click(function(event) {
            if(!grid || grid.getSelecteds().length==0) {
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "error"});
                return false; //验证不通过，不执行以下代码
            }
            /* var data=[],
                selectedItems=grid.getSelecteds();
            for (var i = 0; i < selectedItems.length; i++) {
                data.push(selectedItems[i].id);
            }; */
            var data=[];
            for (var i = 0; i < combo_DATA.length; i++) {
                var combo_item=combo_DATA[i];
                for(key in combo_item){
                    data.push(key);
                }
            };
            //alert(data);
            
            $.ligerDialog.SQconfirm(LogUtils.LOG_CMM_SUBMIT_CONFIRM_CONTENT_001,function (yes) {
                if (yes==false) return;
                if(frameElement && frameElement.dialog) {
                    frameElement.dialog._DATA={usrIds: data}; //给父窗口的dialog传参
                    frameElement.dialog.set("_usrs", combo_DATA); //给父窗口的dialog传参
                    frameElement.dialog.close();
                }
            });
        });
    }
    
  	//初始化查询组织机构查询Form
    function initSearchBarOrgForm(){
    	$("#searchBar2").ligerForm();
    }
    
    //初始化查询组织机构事件
    function initSearchBarOrgFormEvent(){
        $("#searchBtn2").click(function() {
            var form = liger.get("searchBar2");
            var data = form.getData();
            //alert(JSON.stringify(data));
            searchOrgTreeByOrgNm(data);
        });
    }
    
    //清除按钮事件
    function initClearLigerFormEvent(){
    	$("#cleanFormBtn").click(function(event) {
            clearLigerForm("searchBar");
        });
    }
    
    //初始化查询事件
    function initSearchEvent(){
    	$("#searchBtn").click(function(event) {
    		reLoadGrid();
        });
    }
    
    //重新加载GRID
    function reLoadGrid(){
    	var form = liger.get("searchBar");
        var data = form.getData();
        //alert(JSON.stringify(data));
        //var orgId = grid.getSelected().data.id;
        //alert(orgId);
        //加上当前组织
        set_ligergrid(data);
    }
    
    //初始化查询Bar
    function initForm(){
    	$("#searchBar").ligerForm();
    }
    
    //初始化组织树
    function initOrgTree(){
    	 tree = $("#tree1").ligerTree({
             url: '<%=path%>/system/sysOrg/initList.do',
             ajaxType: 'get',
             checkbox: false,
             // single: true,
             parentIcon: "SqFolder",
             childIcon: "SqLeaf",
             needCancel:false,
             isExpand: true,
             isLeaf: function (data) {
                 if (!data) return false;
                 if (data.hasChildren) {return false} else if(data.children){return false} else {return true};
             },
             delay: function(e)
             {
             	<%--  var data = e.data;
                  if (data.hasChildren)
                  {
                      return { url: '<%=path%>/system/sysOrg/treeList.do?id=' + data.id }
                  }
                  return false; --%>
             },
             onAfterAppend: function (parentNode, newdata) {
                 setNanoScroller();
             },
             onExpand: function (node) {
                 setNanoScroller();
             },
             onCollapse: function (node) {
                 setNanoScroller();
             },
             onContextmenu: function (node, e) {
                 actionNode=node;
                 var parentNode = tree.getParent(node);
                 if (parentNode) {
                     //treeMenu.hide();
                     treeMenu2.show({ top: e.pageY, left: e.pageX });
                 }else{  //没有父节点，那就是根节点
                     //treeMenu2.hide();
                     treeMenu.show({ top: e.pageY, left: e.pageX });
                 };
                 return false;
             },
             onSelect: function (node) {
                 //alert(JSON.stringify(node));
                 //alert(node.data.id);
                 set_ligergrid ({"orgId": node.data.id});
             },
             onCancelselect: function (data,target) {
                 if (grid) {
                 	//grid.grid.hide();
                 }
             },
             onSuccess: function (data) {
                 setNanoScroller();
                 tree.selectNode(data[0]);
             }
         });
    }
    
    //通过查询条件获取Tree数据
    function searchOrgTreeByOrgNm (parms) {
        var _url = '<%=path%>/system/sysOrg/searchOrgTree.do';
		AjaxUtils.ajaxGet(_url, parms, function(msg){
			tree.setData(data.data);
   	    },true);
        
    }
    
    //重新处理Grid
    function set_ligergrid (parms) {
        if (grid) {
            grid.grid.show();
			grid.set('parms', {});
            for (name in parms) {grid.setParm(name, parms[name])};
            grid.reload();
        }else{
            render_ligergrid (parms)
        };
    }
    
    //渲染Grid
    function render_ligergrid (parms) {
        grid = $("#ligergrid").ligerGrid({
            url: "<%=path%>/system/sysUsr/searchFunAuthUsrByOrgId.do?usrTypCd=1",
            parms: parms,
            method : 'get',
            fixedCellHeight: false,
            checkbox: true,
            // selectRowButtonOnly: true,
            // isSingleCheck: true,
            frozenCheckbox: false,
            allowHideColumn: false,
            columns: [{
                display: '中文名',
                name: 'refRlNm',
                width: 100
            }, {
                display: '账号',
                name: 'usrNm',
                width: 150
            }, {
                display: '性别',
                name: 'genderCdNm',
                width: 40
            }, {
                display: '组织',
                name: 'orgCdNm',
                width: 200
            }],
            pageSize:10,
            width: '100%',
            isScroll: false,
            root:'rows',
            record:'total',
            hideLoadButton : true,
            //usePagerOnLessPagesize: false,
            onSelectRow: function (rowdata, rowid, rowobj, j) {
                var obj={};
                obj[rowdata.id]=rowdata.refRlNm;
                if(grid.options.isSingleCheck) change_combo_DATA("clear");
                change_combo_DATA("add", obj);
            },
            onUnSelectRow: function (rowdata, rowid, rowobj) {
                var obj={};
                obj[rowdata.id]=rowdata.refRlNm;
                change_combo_DATA("delete", obj);
            },
            onAfterShowData: function() {
                SetIframe_height();
            }
        });
        //开启不同模式,默认模式为多选
        var isMore = $("#isMore").val();
        if(isMore == 0){
        	grid.options.isSingleCheck=true;
        }
    }
    
    
    function change_combo_DATA (method, obj) {
        if (method=="add") {
            for (var i = 0; i < combo_DATA.length; i++) {
                if(JSON.stringify(combo_DATA[i])==JSON.stringify(obj)) {
                    top.$.ligerDialog.SQalert({content: LogUtils.LOG_FLOW_PERSON_ALWAYS_HERE, SQ_type: "warning"});
                    return false;
                }
            };
            combo_DATA.push(obj);
        }
        if (method=="delete") arrDel(obj, combo_DATA);
        if (method=="clear") combo_DATA.length=0;
        view_combo_DATA ();
    }
    function view_combo_DATA () {
        if (combo_DATA.length==0) {
            $("#selecetedItems").addClass('Hide');
            return false;
        };
        var selecetedItems_h=[]
        for (var i = 0; i < combo_DATA.length; i++) {
            var selecetedItem=combo_DATA[i];
            for(key in selecetedItem){
                selecetedItems_h.push('<span>'+selecetedItem[key]+'</span>， ')
            }
        };
        $("#selecetedItems").removeClass('Hide').html(selecetedItems_h.join(''))
    }
    function arrDel (obj,arr) {
        for (var i = 0; i < arr.length; i++) {
            if(JSON.stringify(arr[i])==JSON.stringify(obj)) break;
        };
        arr.splice(i,1);
        return arr;
    }
    function setNanoScroller () {
        $(".nanoWrap .nano").nanoScroller({
            preventPageScrolling: true
        });
    }
    SET_popupIframeHeight();
</script>
</body>
</html>